using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Tile
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D fundo, imgLevel, imgScore, imgVida,barraBaixo;
        Bola bola; 
        enum Estado {INTRO,JOGO,PAUSE};
        Estado estado = Estado.INTRO;   
        Palleta palleta;
        SpriteFont font;

        List<Texture2D> tiles = new List<Texture2D>();
        

        static int tileWidth = 22;
        static int tileHeight = 22;

        int[,] map = {   
                            {8, 8, 8, 8, 8, 8, 3, 8, 8, 5, 5, 5, 8, 1, 8, 8, 0, 8, 8, 8},
                            {8, 8, 1, 1, 1, 1, 8, 3, 8, 8, 8, 5, 8, 8, 8, 0, 0, 0, 8, 8},
                            {6, 8, 8, 1, 1, 8, 8, 8, 4, 4, 8, 8, 8, 8, 0, 0, 0, 0, 0, 8},
                            {6, 8, 1, 1, 1, 1, 8, 8, 4, 4, 4, 4, 8, 8, 8, 0, 0, 0, 8, 8}, 
                            {6, 8, 8, 8, 8, 8, 5, 8, 8, 8, 8, 8, 8, 0, 8, 8, 0, 8, 8, 8},
                            {6, 8, 8, 2, 8, 4, 8, 3, 8, 8, 8, 8, 3, 8, 8, 8, 8, 8, 8, 8},
                            {8, 8, 2, 2, 2, 8, 6, 8, 3, 3, 3, 3, 8, 8, 7, 7, 7, 7, 7, 8},
                            {8, 2, 2, 2, 2, 2, 8, 8, 3, 3, 3, 3, 8, 8, 7, 8, 8, 8, 8, 8},
                            {8, 8, 2, 2, 2, 8, 8, 3, 8, 8, 8, 8, 3, 8, 8, 8, 8, 2, 0, 8},
                            {8, 8, 8, 2, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 1, 3, 8}
                        };
 

         public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            graphics.PreferredBackBufferWidth = 420; 
            graphics.PreferredBackBufferHeight = 450;
            Window.Title = "Arkanoid";
            graphics.ApplyChanges();  
            

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            tiles.Add(Content.Load<Texture2D>(@"Blocos\Bloco_1"));
            tiles.Add(Content.Load<Texture2D>(@"Blocos\Bloco_2"));
            tiles.Add(Content.Load<Texture2D>(@"Blocos\Bloco_3"));
            tiles.Add(Content.Load<Texture2D>(@"Blocos\Bloco_4"));
            tiles.Add(Content.Load<Texture2D>(@"Blocos\Bloco_5"));
            tiles.Add(Content.Load<Texture2D>(@"Blocos\Bloco_6"));
            tiles.Add(Content.Load<Texture2D>(@"Blocos\Bloco_7"));
            tiles.Add(Content.Load<Texture2D>(@"Blocos\Bloco_8"));
            tiles.Add(Content.Load<Texture2D>(@"Blocos\Bloco_10"));
            tiles.Add(Content.Load<Texture2D>(@"Blocos\Bloco_"));

            font = Content.Load<SpriteFont>("Fonte");   
            fundo = Content.Load<Texture2D>(@"Imagens\fundo");
            imgLevel = Content.Load<Texture2D>(@"Imagens\level");
            imgScore = Content.Load<Texture2D>(@"Imagens\score");
            imgVida = Content.Load<Texture2D>(@"Imagens\Vida");   
            barraBaixo = Content.Load<Texture2D>(@"Imagens\BarraBaixo");
            bola = new Bola(Window, Content.Load<Texture2D>(@"Imagens\bola"), new Vector2(), Content.Load<SoundEffect>(@"Som\pong1"));        
            palleta = new Palleta(Content.Load<Texture2D>(@"Imagens\Barra1"), new Vector2(Window.ClientBounds.Width / 2, 390)); 

            // TODO: use this.Content to load your game content her
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            palleta.Update(Window, new List<Keys>(){Keys.Right, Keys.Left});
            bola.Update(Window, gameTime, palleta);    

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            // TODO: Add your drawing code here

            spriteBatch.Begin();
            spriteBatch.Draw(fundo,new Vector2(0,0) ,Color.White);     
           


                for (int y = 0; y < map.GetLength(0); y++)
                {
                    for (int x = 0; x < map.GetLength(1); x++)
                    {
                        spriteBatch.Draw(tiles[map[y, x]] , new Rectangle(x * tileWidth, y * tileHeight, tileWidth, tileHeight),Color.White);        
                    }
                }   



            palleta.Draw(spriteBatch);

            spriteBatch.Draw(barraBaixo, new Vector2(0, 425), Color.White);  
            spriteBatch.Draw(imgVida,new Vector2(0,427), Color.White); 
            spriteBatch.Draw(imgLevel,new Vector2(250,427), Color.White);
            spriteBatch.Draw(imgScore,new Vector2(100,427), Color.White);
            spriteBatch.DrawString(font, ":", new Vector2(26, 426), Color.Silver);
            spriteBatch.DrawString(font, ":", new Vector2(175, 426), Color.Silver);
            spriteBatch.DrawString(font, ":", new Vector2(322,426), Color.Silver);
            spriteBatch.DrawString(font, "000", new Vector2(33,427), Color.Silver);
            spriteBatch.DrawString(font, "000", new Vector2(183,427), Color.Silver);
            spriteBatch.DrawString(font, "000", new Vector2(330,427), Color.Silver);

            bola.Draw(spriteBatch);   

            spriteBatch.End();
  
                base.Draw(gameTime);
        }
    }
}
